import axios from 'axios';
const iconv = require('iconv-lite')
import hushen300 from '../config/hushen300';
import { getDongfangcaifuUrl } from '../config';
import CrawlerThread from './crawlerThread';
import * as fs from 'fs';


// 爬取沪深300日K数据
const crawlerHuShen300 = () => {
  // 沪深300股票代码
  const stockIds = Object.keys(hushen300);
  const crawlerWorks = [];
  stockIds.forEach((stockId) => {
    const url = getDongfangcaifuUrl(stockId);
    crawlerWorks.push(
      new CrawlerThread({
        lazyTime: 60 * 1000,
        callBackFn: async () => {
          await axios({
            method: 'get',
            url,
            responseType: 'stream'
          }).then(res => {
            const chunks = [];
            res.data.on('data', chunk => {
              chunks.push(chunk);
            });
            res.data.on('end', () => {
              const buffer = Buffer.concat(chunks)
              const str = iconv.decode(buffer, 'gbk')
              fs.appendFileSync('./gupiao.txt', str);
            })
          });
        }
      })
    );

    // -------------------------test-------------------------------------
    // axios({
    //   method: 'get',
    //   url,
    //   responseType: 'stream'
    // }).then(res => {
    //   const chunks = [];
    //   res.data.on('data', chunk => {
    //     chunks.push(chunk);
    //   });
    //   res.data.on('end', () => {
    //     const buffer = Buffer.concat(chunks)
    //     const str = iconv.decode(buffer, 'gbk')
    //     fs.appendFileSync('./gupiao.txt', `${stockId}: ${hushen300[stockId]}\n`);
    //     fs.appendFileSync('./gupiao.txt', `${url}\n`);
    //     fs.appendFileSync('./gupiao.txt', str);
    //     fs.appendFileSync('./gupiao.txt', '\n');
    //   })
    // });
    // -------------------------test-------------------------------------
  });
  crawlerWorks.forEach(crawlerWork => {
    setInterval(() => {
      crawlerWork.startThread();
    }, crawlerWork.getLazyTime());
  });
};

export {
  crawlerHuShen300
}